CC=g++
CFLAGS=-Icontrib/install/include/ -Icontrib/install/include/thrift -Isrc/ -Isrc/thrift
LDFLAGS=-Lcontrib/install/lib/ -lcurl -lleveldb -lsnappy -lthrift \
        -lboost_regex -lboost_thread -lssl -lrt -lz

ifdef NDEBUG
CFLAGS += -DNDEBUG -O2
else
CFLAGS += -g
endif

COMMON_SRC=\
	src/thrift/CrawlerServer.h \
	src/thrift/CrawlerServer.cpp \
	src/thrift/fetcher_server_constants.h \
	src/thrift/fetcher_server_constants.cpp \
	src/thrift/fetcher_server_types.h \
	src/thrift/fetcher_server_types.cpp \
	src/config.h \
	src/config.cpp \
	src/url.h \
	src/url.cpp \
	src/utils.h \
	src/utils.cpp \
	src/parser.h \
	src/parser.cpp \
	src/server.h \
	src/server.cpp \
	src/store.h \
	src/store.cpp \
	src/fetcher.h \
	src/fetcher.cpp \

SERVER_MAIN_SRC=\
	src/server_main.cpp \

CLIENT_MAIN_SRC=\
	src/client_main.cpp \

TEST_SRC=\
	test/regex_test.cpp \
	test/server_test.cpp \

COMMON_CPP=$(filter %.cpp, $(COMMON_SRC))
TEST_CPP=$(filter %.cpp, $(TEST_SRC))

COMMON_OBJ=$(COMMON_CPP:.cpp=.o)
TEST_OBJ=$(TEST_CPP:.cpp=.o)
SERVER_MAIN_OBJ=$(SERVER_MAIN_SRC:.cpp=.o)
CLIENT_MAIN_OBJ=$(CLIENT_MAIN_SRC:.cpp=.o)

.PHONY:thrift_gen
TARGET=bin/fetcher bin/fetcherd bin/test
all:$(TARGET)

bin/fetcherd:$(COMMON_OBJ) $(SERVER_MAIN_OBJ)
	$(CC) -o $@ $^ $(LDFLAGS)
bin/fetcher:$(COMMON_OBJ) $(CLIENT_MAIN_OBJ)
	$(CC) -o $@ $^ $(LDFLAGS)
bin/test:$(COMMON_OBJ) $(TEST_OBJ)
	$(CC) -o $@ $^ $(LDFLAGS) -lgtest_main
thrift_gen:src/thrift/fetcher_server.thrift
	thrift -gen cpp -gen php $<
	mv -f gen-cpp/* src/thrift
	rm -fr gen-cpp
%.o:%.cpp
	$(CC) -c $(CFLAGS) -o $@ $<
clean:
	rm -fr $(TARGET) $(COMMON_OBJ) $(SERVER_MAIN_OBJ) \
	       $(CLIENT_MAIN_OBJ) $(TEST_OBJ)
	touch src/thrift/fetcher_server.thrift

